<?php
namespace App\Helpers;

class StdArray{
    /**
     * ????????$arr?У????key??$key????????????????????????????????
     * ????
     *  $arr = [
     *          [
     *              'name' => 'li',
     *              'sex' => 'male'
     *          ],
     *          [
     *             'name' => 'zhang',
     *             'sex' => 'male'
     *          ],
     *          [
     *              'name' => 'chen',
     *              'sex' => 'female'
     *          ]
     *      ];
     *  $data = StdArray::uniqueValues($arr, 'sex');
     * ?? $data = [
     *      'male',
     *      'female'
     * ];
     * @param $arr
     * @param $key
     * @return array
     */
    public static function uniqueValues($arr,$key){
        $data = [];
        if($arr) {
            foreach($arr as $v){
                $data[] = $v[$key];
            }
        }
        return array_unique( $data );
    }

    /**
     * ????????$arr???????????
     *  ???key?$key???????????????key?????key? $value???????????????value
     * ????
     *  $arr = [
     *          [
     *              'name' => 'li',
     *              'sex' => 'male'
     *          ],
     *          [
     *             'name' => 'zhang',
     *             'sex' => 'male'
     *          ],
     *          [
     *              'name' => 'chen',
     *              'sex' => 'female'
     *          ]
     *      ];
     * $data = StdArray::indexedValues($arr, 'name', 'sex');
     *  ??$data = [
     *          'li' => 'male',
     *          'zhang' => 'male',
     *          'chen'  => 'female'
     *  ]
     * @param $arr
     * @param $key
     * @param $value
     * @return array
     */
    public static function indexedValues($arr,$key,$value){
        $data = [];
        if($arr){
            foreach($arr as $v){
                $data[$v[$key]] = $v[$value];
            }
        }
        return $data;
    }

    /**
     * ???λ???? $arr ??????? key?$key???????????????????
     * ????
     * $arr = [
     *          [
     *              'name' => 'li',
     *              'sex' => 'male',
     *              'age' => '12'
     *          ],
     *          [
     *             'name' => 'zhang',
     *             'sex' => 'male',
     *               'age' => '17'
     *          ],
     *          [
     *              'name' => 'chen',
     *              'sex' => 'female',
     *               'age' => '22'
     *          ]
     *      ];
     * $data = StdArray::indexed($arr, 'age');
     * ?? $data = [
     *          '12' => [
     *              'name' => 'li',
     *              'sex' => 'male',
     *              'age' => '12'
     *          ],
     *          '17' => [
     *             'name' => 'zhang',
     *             'sex' => 'male',
     *               'age' => '17'
     *          ],
     *          '22' => [
     *              'name' => 'chen',
     *              'sex' => 'female',
     *               'age' => '22'
     *          ]
     *      ];
     * @param $arr
     * @param $key
     * @return array
     */
    public static function indexed($arr,$key){
        $data = [];
        if($arr){
            foreach($arr as $v){
                $data[$v[$key]] = $v;
            }
        }
        return $data;
    }
    /**
     * ???λ???? $arr ???????????????
     * ????
     * $arr = [
     *          [
     *              'name' => 'li',
     *              'sex' => 'male',
     *              'age' => '12'
     *          ],
     *          [
     *             'name' => 'zhang',
     *             'sex' => 'male',
     *               'age' => '17'
     *          ],
     *          [
     *              'name' => 'chen',
     *              'sex' => 'female',
     *               'age' => '22'
     *          ]
     *      ];
     * $data = StdArray::groupIndexed($arr, 'sex', 'age');
     * ?? $data = [
     *          'male' =>
     *              [
     *              '12' => [
     *                  'name' => 'li',
     *                   'sex' => 'male',
     *                   'age' => '12'
     *               ],
     *               '17' => [
     *                  'name' => 'zhang',
     *                  'sex' => 'male',
     *                  'age' => '17'
     *              ]
     *          ],
     *          'female'=>[
     *              '22' => [
     *                  'name' => 'chen',
     *                  'sex' => 'female',
     *                  'age' => '22'
     *              ]
     *          ]
     *      ];
     * @param $arr
     * @param $key
     * @return array
     */
    public static function groupIndexed($data, $cou_id, $sk_id){
        $quna = [];
        foreach ($data as $k => $v) {
            $quna[$v[$cou_id]][$v[$sk_id]] = $v;
        }
        return $quna;
    }
    /**
     * ???λ???? $arr ??????????????
     * ????
     * $arr = [
     *          [
     *              'name' => 'li',
     *              'sex' => 'male',
     *              'age' => '12'
     *          ],
     *          [
     *             'name' => 'zhang',
     *             'sex' => 'male',
     *               'age' => '17'
     *          ],
     *          [
     *              'name' => 'chen',
     *              'sex' => 'female',
     *               'age' => '22'
     *          ]
     *      ];
     * $data = StdArray::groupIndexed($arr, 'sex', 'age');
     * ?? $data = [
     *          'male' =>
     *              [
     *              '12' => '12'
     *              '17' => '17'
     *          ],
     *          'female'=>[
     *              '22' => '22'
     *          ]
     *      ];
     * @param $arr
     * @param $key
     * @return array
     */
    public static function groupedValues($data, $cou_id, $sk_id){
        foreach ($data as $k => $v) {
            $quna[$v[$cou_id]][$v[$sk_id]]=$v[$sk_id];
        }
        return $quna;

    }

    /**
     * 获取数组中的某个key的所有值
     * @param $list
     * @param $key
     * @return array
     */
    public static function getValues($list,$key){
        $data = [];
        foreach ($list as $value){
            $data[] = $value[$key];
        }
        return $data;
    }
}
